/* eslint-disable */
class polygonSelect {
    // 构造函数
    constructor(rangePoints, checkPointList) {
        this.rangePoints = rangePoints
        this.rangePoints.push(this.rangePoints[0])
        // console.log(this.rangePoints)
        this.checkPointList = checkPointList
    }
    // 计算受检点集checkPointList是否在检测面rangePoints范围中
    main() {
        // 四至截取
        let pointsIn4Boundary = this.MaxAndMin()
        // 射线求交
        let flag = this.RayIntersection(pointsIn4Boundary, this.rangePoints)
        return flag
    }
    // 四至截取
    MaxAndMin() {
        let approvedPoints = [];
        //  读取四至
        let lngMax = 0;
        let lngMin = 9999999999999999;
        let latMax = 0;
        let latMin = 999999999999999;
        this.rangePoints.forEach((point) => {
            let lng = point[0];
            let lat = point[1];
            if (lng > lngMax) lngMax = lng;
            if (lng < lngMin) lngMin = lng;
            if (lat > latMax) latMax = lat;
            if (lat < latMin) latMin = lat;
        });
        // console.log(lngMax, lngMin, latMax, latMin);
        //  读取四至内的企业点
        this.checkPointList.forEach((element) => {
            let x = element[0];
            let y = element[1];
            if (x < lngMax && x > lngMin && y < latMax && y > latMin) {
                approvedPoints.push(element);
            }
        });
        // console.log(approvedPoints)
        return approvedPoints;
    }
    // 射线求交
    RayIntersection(pointsIn4Boundary, rangePoints) {
        let selection = [];
        // 遍历四至截取后的点,记录水平交点数量
        pointsIn4Boundary.forEach((element) => {
            let count = 0;
            let points = [];
            for (let i = 0; i < rangePoints.length - 1; i++) {
                //  确认当前边界点与前一边界点y值的高低
                let latMax, latMin;
                if (rangePoints[i][0] > rangePoints[i + 1][0]) {
                    latMax = rangePoints[i][0];
                    latMin = rangePoints[i + 1][0];
                } else {
                    latMax = rangePoints[i + 1][0];
                    latMin = rangePoints[i][0];
                }
                //   若y值在当前边界点y值之间,则认为是相交的
                // console.log(rangePoints[i], rangePoints[i + 1])
                // console.log(element, latMin, latMax)
                if (element[0] > latMin && element[0] < latMax) {
                    // 更新相交计数
                    count++;
                    // 将当前边界点与前一边界点保存
                    points.push([rangePoints[i], rangePoints[i + 1]]);
                }
            }
            // 若与边界无相交则跳过
            if (count != 0) {
                let right = 0;
                let left = 0;
                points.forEach((point) => {
                    let x = this.boundaryFunction(point, element[0]);
                    // console.log(x, element[1])
                    x > element[1] ? right++ : left++;
                });
                // console.log(right, left);
                //  奇数偶数判断,若左右皆奇数则认为在选区内
                if (right % 2 != 0 && left % 2 != 0) {
                    selection.push(element);
                }
            }
        });
        // console.log(selection)
        return selection.length > 0;
    }
    //  求算边界线函数,根据边界函数求算相交点的x值
    boundaryFunction(boundary, y) {
        // console.log(boundary, y);
        let point1 = {
            x: boundary[0][1],
            y: boundary[0][0],
        };
        let point2 = {
            x: boundary[1][1],
            y: boundary[1][0],
        };
        let top = point2.x * (y - point1.y) + point1.x * (point2.y - y);
        let bottom = point2.y - point1.y;
        let x = top / bottom;
        //   console.log([y, x]);
        return x;
    }
}

// 导出polygonSelect类
export { polygonSelect }